package org.htzb.news.web;

import org.htzb.news.dao.NewsDao;
import org.htzb.news.dao.TopicDao;
import org.htzb.news.dao.impl.NewDaoImpl;
import org.htzb.news.dao.impl.TopicDaoImpl;
import org.htzb.news.entity.News;
import org.htzb.news.entity.Topic;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/NewsServlet")

public class NewsServlet extends HttpServlet {
    NewsDao newsDao=new NewDaoImpl();
    TopicDao topicDao=new TopicDaoImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//设置请求编码格式
       String opr= req.getParameter("opr");
       if(opr==null){
           opr="list";
       }
       if(opr.equals("readNews")){
           //查看新闻详情
           String id = req.getParameter("id");
           if(id!=null&&id.length()>0){
               try {
                   News news=newsDao.getNewsById(Integer.parseInt(id));
                   req.getSession().setAttribute("news", news);
                   resp.sendRedirect("news_read.jsp");
               } catch (SQLException e) {
                   throw new RuntimeException(e);
               }
           }

       }else if (opr.equals("list")){
           try {
               List<News> newsList = newsDao.getNews(-1);//获取所有新闻列表
               List<Topic> topicList = topicDao.getAllTopic();//获取所有新闻主题

               List<News> homeList = newsDao.getNews(1);//获取所有国内新闻列表
               List<News> externaList = newsDao.getNews(2);//获取所有国际新闻列表
               List<News> entertainmentList = newsDao.getNews(5);
               req.getSession().setAttribute("homeList", homeList);
               req.getSession().setAttribute("externaList", externaList);
               req.getSession().setAttribute("entertainmentList", entertainmentList);

               req.getSession().setAttribute("topicList", topicList);
               req.getSession().setAttribute("topicNewsList", newsList);
               resp.sendRedirect("index.jsp");
           } catch (SQLException e){
               throw new ServletException(e);
           }
       }else if(opr.equals("topicNews")){
           //查询新闻类别下的新闻列表
           String tid=req.getParameter("tid");
           if (tid!=null&&tid.length()>0){
               try {
                   List<News> newsList = newsDao.getNews(Integer.parseInt(tid));
                   req.getSession().setAttribute("topicNewsList", newsList);
                   resp.sendRedirect("index.jsp");
               } catch (SQLException e) {
                   throw new RuntimeException(e);
               }
           }
       }else if (opr.equals("admin")){
           resp.sendRedirect(req.getContextPath()+"/newspages/admin.jsp");

       }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
